package com.manage.adapter;

import java.util.ArrayList;

import com.manage.HistoryOrderDetailActivity;
import com.manage.ManageAcceptedActivity;
import com.manage.OrderDetailActivity;
import com.manage.R;
import com.manage.model.Order;
import com.manage.model.OrderItem;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

/**
 * 统计中订单详情listview的item设置
 * @author liyoro
 * @date 2012-6-19
 */
public class HistoryOrderAdapter extends BaseAdapter {
	private LayoutInflater layoutInflater;
	private ArrayList<Order> orderList = new ArrayList();
	private Context context;

	private boolean clearList;
	
	public HistoryOrderAdapter(Context paramContext) {
		this.context = paramContext;
		this.layoutInflater = LayoutInflater.from(paramContext);
	}
	
	@Override
	public int getCount() {
		return this.orderList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.orderList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder localViewHolder;
		final Order localOrder;

		if (convertView == null) {
			localViewHolder = new ViewHolder();
			
			convertView = this.layoutInflater.inflate(R.layout.item_accounted_order_detail, null);
			localViewHolder.clientName = (TextView) convertView.findViewById(R.id.client_name);
			localViewHolder.orderTime = (TextView) convertView.findViewById(R.id.order_time);
			localViewHolder.phoneNumber = (TextView) convertView.findViewById(R.id.phone_number);
			localViewHolder.clientAddress = (TextView) convertView.findViewById(R.id.client_address);
			localViewHolder.clientAddition = (TextView) convertView.findViewById(R.id.client_other);
			localViewHolder.orderContent = (TextView) convertView.findViewById(R.id.order_content);
			
			localViewHolder.tvStatus = (TextView) convertView.findViewById(R.id.btn_modify_order);
			localViewHolder.btnDetail = (Button) convertView.findViewById(R.id.btn_delivery_order);
			
			convertView.setTag(localViewHolder);
		} else {
			localViewHolder = (ViewHolder) convertView.getTag();
		}
		
		localOrder = (Order)getItem(position);
		
		localViewHolder.clientName.setText(localOrder.getClientName());
		localViewHolder.orderTime.setText(localOrder.getOrderTime());
		localViewHolder.phoneNumber.setText(localOrder.getPhoneNumber());
		localViewHolder.clientAddress.setText(localOrder.getClientAddress());
		localViewHolder.clientAddition.setText(localOrder.getClientAddition());
		
		String content = "";
		
		ArrayList<OrderItem> dish = localOrder.getDishes();
		for(int i=0; i<dish.size(); i++) {
			OrderItem item = dish.get(i);
			
			content += item.getCaipin_name() + item.getCaipin_count() +"份； ";
		}
		localViewHolder.orderContent.setText(content);
		
		if(localOrder.getStatus().equals("1")) {
			localViewHolder.tvStatus.setText("待受理");
		} 
		else if(localOrder.getStatus().equals("2")){
			localViewHolder.tvStatus.setText("待配送");
		} 
		else if(localOrder.getStatus().equals("3")){
			localViewHolder.tvStatus.setText("待入账");
		} 
		else if(localOrder.getStatus().equals("4")){
			localViewHolder.tvStatus.setText("已完成");
		} 
		else if(localOrder.getStatus().equals("5")){
			localViewHolder.tvStatus.setText("已取消");
		}
		
		localViewHolder.btnDetail.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 跳转到订单详细页面
				Intent intent = new Intent();
				intent.setClass(context, HistoryOrderDetailActivity.class);
				
				// 通过Bundle来获取数据,通过key-Value的方式放入数据
				Bundle bundle = new Bundle();
				bundle.putSerializable(ManageAcceptedActivity.SER_KEY, localOrder);
				intent.putExtras(bundle);   
				
				context.startActivity(intent);
			}
		});
		
		return convertView;
	}
	
	public void setValue(ArrayList<Order> paramArrayList) {
		if(getClear()) {
			this.orderList.clear();
		}
		
		if(paramArrayList != null) {
			this.orderList.addAll(paramArrayList);
		}
		notifyDataSetChanged();
	}

	/**
	 * 设置是否清空list中数据，防止分页数据没了
	 * @param isClear
	 */
	public void setClear(boolean isClear) {
		this.clearList = isClear;
	}
	
	public boolean getClear() {
		return this.clearList==true?true:false;
	}
	
	static class ViewHolder {
		TextView orderTime; // 下单时间
		TextView clientName; // 客户名
		TextView phoneNumber; // 客户电话号码
		TextView clientAddress; // 送餐地址
		TextView clientAddition; // 补充
		TextView orderContent; // 订餐内容

		TextView tvStatus; //订单状态
		Button btnDetail;
	}
}
